cmake_minimum_required(VERSION 2.8.2)
project(ProjectName)

set(CMAKE_BUILD_TYPE debug)
MESSAGE( STATUS "变量:CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")
add_compile_options(-std=c++11)

find_package( OpenCV REQUIRED )
MESSAGE( STATUS "变量:OpenCV_LIBS = ${OpenCV_LIBS}")
MESSAGE( STATUS "变量:OpenCV_INCLUDE_DIRS = ${OpenCV_INCLUDE_DIRS}")
find_package( OpenGL )
MESSAGE( STATUS "变量:OPENGL_FOUND = ${OPENGL_FOUND}")
MESSAGE( STATUS "变量:OPENGL_INCLUDE_DIRS = ${OPENGL_INCLUDE_DIRS}")
if(OPENGL_FOUND)
    include_directories( ${OpenCV_INCLUDE_DIRS} ${OPENGL_INCLUDE_DIRS} )
else()
    include_directories( ${OpenCV_INCLUDE_DIRS} )
endif()

macro(SUBDIRLIST result curdir)
    file(GLOB children RELATIVE ${curdir} ${curdir}/*)
    set(dirlist "")
    foreach(child ${children})
        if(IS_DIRECTORY ${curdir}/${child})
            LIST(APPEND dirlist ${child})
        endif()
    endforeach()
    set(${result} ${dirlist})
endmacro()

function(SUBFILELIST result curdir pattern)
    file(GLOB children RELATIVE ${curdir} ${curdir}/*)
    set(filelist "")
    foreach(child ${children})
        if(NOT IS_DIRECTORY ${curdir}/${child} AND child MATCHES "${pattern}")
            STRING(REGEX REPLACE ".cpp$" "" child_h ${child})
            LIST(APPEND filelist ${child_h})
        endif()
    endforeach()
    set(${result} ${filelist} PARENT_SCOPE)
endfunction()

MESSAGE( STATUS "变量match:${CMAKE_SOURCE_DIR}/src/cpp")
SUBFILELIST(SubFileMatch ${CMAKE_SOURCE_DIR}/src/cpp ".*cpp")
foreach(ITEM_FILE ${SubFileMatch})
    message("项目：= ${ITEM_FILE}")
    set(TARGET "${ITEM_FILE}")
    add_executable(${TARGET} ${CMAKE_SOURCE_DIR}/src/cpp/${ITEM_FILE}.cpp)
    target_link_libraries(${TARGET} ${OpenCV_LIBS} )
endforeach()
